using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoblinXNA.SceneGraph;
using GoblinXNA.Graphics;
using Microsoft.Xna.Framework;
using GoblinXNA.Graphics.Geometry;
using GoblinXNA.Physics;

namespace SplashOn_PhoneLib
{
    class Class1
    {
        public static void creatVirtualCanvas(TransformNode groundNode,
                                                float virtualCanvasXdim,
                                                float virtualCanvasYdim,
                                                float virtualCanvasZdim,
                                                float canvasTransparency,
                                                int virtualCanvasOverlaysXitems,
                                                int virtualCanvasOverlaysYitems
                                                )
        {
            /* for creating the virtual canvas and the transparent RED (semi - transparent) overlay */
            // Since material is common it can be outside the other code. // TODO - Check for problems with referenceing LATER.
            Material virtualCanvasOverlayNodeMaterial = new Material();
            Color c = new Color(Color.White.ToVector4().X,
                                Color.White.ToVector4().Y,
                                Color.White.ToVector4().Z,
                                canvasTransparency);
            virtualCanvasOverlayNodeMaterial.Diffuse = c.ToVector4();
            virtualCanvasOverlayNodeMaterial.Ambient = c.ToVector4();
            virtualCanvasOverlayNodeMaterial.Specular = c.ToVector4();
            virtualCanvasOverlayNodeMaterial.SpecularPower = 10;

            // Creating the Canvas Node.
            GeometryNode virtualCanvasNode = new GeometryNode("virtualCanvasNode");
            virtualCanvasNode.Model = new Box(virtualCanvasXdim, virtualCanvasYdim, virtualCanvasZdim);
            virtualCanvasNode.AddToPhysicsEngine = true;
            virtualCanvasNode.Physics.Collidable = true;
            virtualCanvasNode.Physics.Shape = ShapeType.Box;
            virtualCanvasNode.Physics.MaterialName = "Canvas";
            Material virtualCanvasNodeMaterial = new Material();

            virtualCanvasNodeMaterial.Diffuse = Color.White.ToVector4();
            virtualCanvasNodeMaterial.Ambient = Color.White.ToVector4();
            virtualCanvasNodeMaterial.Specular = Color.White.ToVector4();
            virtualCanvasNodeMaterial.SpecularPower = 10;
            virtualCanvasNode.Material = virtualCanvasNodeMaterial;
            TransformNode virtualCanvasNodeTransNode = new TransformNode("virtualCanvasNodeTransNode");
            virtualCanvasNodeTransNode.AddChild(virtualCanvasNode);
            virtualCanvasNodeTransNode.Translation = new Vector3(0, 0, 0);
            groundNode.AddChild(virtualCanvasNodeTransNode);

            // Creating the Overlay Node ....
            GeometryNode[,] virtualCanvasOverlayNodes =
                new GeometryNode[virtualCanvasOverlaysXitems, virtualCanvasOverlaysYitems];
            TransformNode[,] virtualCanvasOverlayTransNodes =
                new TransformNode[virtualCanvasOverlaysXitems, virtualCanvasOverlaysYitems];
            float virtualCanvasOverlayNodesXdim = virtualCanvasXdim / virtualCanvasOverlaysXitems;
            float virtualCanvasOverlayNodesYdim = virtualCanvasYdim / virtualCanvasOverlaysYitems;
            for (int i = 0; i < virtualCanvasOverlaysXitems; ++i)
            {
                for (int j = 0; j < virtualCanvasOverlaysYitems; ++j)
                {
                    virtualCanvasOverlayNodes[i, j] = new GeometryNode();
                    virtualCanvasOverlayNodes[i, j].Model = new Box(virtualCanvasOverlayNodesXdim,
                        virtualCanvasOverlayNodesYdim, 1);
                    virtualCanvasOverlayNodes[i, j].AddToPhysicsEngine = true;
                    virtualCanvasOverlayNodes[i, j].Physics.Collidable = true;
                    virtualCanvasOverlayNodes[i, j].Physics.Shape = ShapeType.Box;
                    virtualCanvasOverlayNodes[i, j].Material = virtualCanvasOverlayNodeMaterial;
                    virtualCanvasOverlayNodes[i, j].Physics.MaterialName = "Canvas";
                    virtualCanvasOverlayTransNodes[i, j] = new TransformNode();
                    virtualCanvasOverlayTransNodes[i, j].AddChild(virtualCanvasOverlayNodes[i, j]);
                    float x = virtualCanvasXdim / 2 * -1 + virtualCanvasXdim / (virtualCanvasOverlaysXitems * 2);
                    float y = virtualCanvasYdim / 2 * -1 + virtualCanvasYdim / (virtualCanvasOverlaysYitems * 2); ;
                    //Console.WriteLine("x: " + x + ", y: " + y);
                    virtualCanvasOverlayTransNodes[i, j].Translation =
                        new Vector3(x + i * virtualCanvasOverlayNodesXdim, y + j * virtualCanvasOverlayNodesYdim, 5);
                    DebugHelper.w("Adding Overlay Item - " + i + ", " + j + " :: " + virtualCanvasOverlayTransNodes[i, j].Translation.ToString());
                    groundNode.AddChild(virtualCanvasOverlayTransNodes[i, j]);
                }
            }
            /* end of creating virtual canvas */
        }
    }
}
